$(function () {
    Time();
    load();
    cha();
})

function fh() {
    location.href = "../../tan_kai/Dmgg.html";
    $(".ad").removeAttr("hidden");
    $("#XQ").removeAttr("hidden");
    Time();
    load();
}

let list = [];

function cha() {
    let DfileOnt = JSON.parse(localStorage.getItem("DfileOnt"));
    $.post("/Dmoduledetails.do/listxb", {ParentId: DfileOnt.designid}, function (sdata) {
        list = sdata;
        let str = " <tr >\n" +
            "           <th>点选</th>\n" +
            "           <th>物料名称</th>\n" +
            "           <th>物料编号</th>\n" +
            "           <th>用途类型</th>\n" +
            "           <th>数量</th>\n" +
            "           <th>单位</th>\n" +
            "           <th>单价(元)</th>\n" +
            "           <th>小计(元)</th>\n" +
            "       </tr>\n";
        $.each(list, function (index, item) {
            str +=
                "  <tr>\n" +
                "      <td><input type=\"checkbox\" name='checkbox' value='" + item.productid + "'></td>\n" +
                "      <td>" + item.productname + "</td>\n" +
                "      <td>" + item.productid + "</td>\n" +
                "      <td>" + (item.type == 'Y001-1' ? '商品' : item.type == 'Y001-2' ? '物料' : '') + "</td>\n" +
                "      <td><input type=\"number\" value='" + item.amount + "' class=\"amount\"  style=\"border: none;height: 100%;width: 100%;\"></td>\n" +
                "      <td><span class='amountunit' >" + item.amountunit + "</span></td>\n" +
                "      <td><span class='costprice' >" + item.costprice + "</span></td>\n" +
                "      <td><span class='xiaoji'>" + item.subtotal + "</span></td>\n" +
                "  </tr>";
        })
        $("#tableAdd").html(str);
    })
}

function Time() {
    var time = new Date();
    var y = time.getFullYear();
    var mon = time.getMonth() + 1; //0-11
    var d = time.getDate();
    var h = time.getHours();
    var m = time.getMinutes();
    var s = time.getSeconds();
    $("#datetime").val(y + "-" + mon + "-" + d + " " + h + ":" + m + ":" + s);
}

function load() {
    let u = localStorage.getItem("user");
    let user = JSON.parse(u);
    $("#name").val(user.rname);
    let DfileOnt = JSON.parse(localStorage.getItem("DfileOnt"));
    $("#productname").val(DfileOnt.productname);
    $("#productid").val(DfileOnt.productid);
    $("#sjr").val(DfileOnt.designer);
    $("#remark").val(DfileOnt.moduledescribe);

    let str = " <tr >\n" +
        "           <th>点选</th>\n" +
        "           <th>物料名称</th>\n" +
        "           <th>物料编号</th>\n" +
        "           <th>用途类型</th>\n" +
        "           <th>数量</th>\n" +
        "           <th>单位</th>\n" +
        "           <th>单价(元)</th>\n" +
        "           <th>小计(元)</th>\n" +
        "       </tr>\n";
    $.each(list, function (index, item) {
        str +=
            "  <tr>\n" +
            "      <td><input type=\"checkbox\" name='checkbox' value='" + item.productid + "'></td>\n" +
            "      <td>" + item.productname + "</td>\n" +
            "      <td>" + item.productid + "</td>\n" +
            "      <td>" + (item.type == 'Y001-1' ? '商品' : item.type == 'Y001-2' ? '物料' : '') + "</td>\n" +
            "      <td><input type=\"number\" value='" + item.amount + "' class=\"amount\"  style=\"border: none;height: 100%;width: 100%;\"></td>\n" +
            "      <td><span class='amountunit' >" + item.amountunit + "</span></td>\n" +
            "      <td><span class='costprice' >" + item.costprice + "</span></td>\n" +
            "      <td><span class='xiaoji'>" + item.subtotal + "</span></td>\n" +
            "  </tr>";
    })
    $("#tableAdd").html(str);
}

function Del() {
    let ids = [];
    $("[name='checkbox']:checked").each(function () {
        ids.push($(this).val());
    })
    if (ids.length > 1 || ids.length == 0) {
        return;
    } else {
        let ls = [];
        for (let i = 0; i < list.length; i++) {
            if (list[i].productid != ids[0]) {
                ls[i] = list[i];
            }
        }
        list = ls;
        load();
    }
}

function add() {
    $("#AccRoleModal").modal("show");
    page();
    loadpage();
}

function page() {
    $("#tm01").bootstrapTable({
        //请求方式  get请求中文会乱码，建议使用post请求
        method: 'post',
        //请求头部的设置
        contentType: 'application/json',
        //url
        url: '/Dflile.do/pagewl',
        //设置交替行样式
        striped: true,
        //初始显示页号
        pageNumber: 1,
        //设置表格高度
        //height: 350,
        //是否显示分页
        pagination: true,
        //设置是服务端分页还是客户端分页
        sidePagination: 'server',
        //页大小
        pageSize: 5,
        //分页列表
        pageList: [3, 5, 6, 10],
        //上传到服务端的参数
        queryParams: function (params) {
            return {
                num: params.offset / params.limit + 1,
                size: params.limit
            }
        },
        // 定义列名
        columns: [
            {title: "物料编号", field: "productid", align: "center"},
            {title: "物料名称", field: "productname", align: "center"},
            {
                title: "用途类型", field: "type", align: "center",
                formatter: function (vale, row, index) {
                    return vale == 'Y001-1' ? "商品" : vale == 'Y001-2' ? "物料" : ""
                }
            },
            {title: "物料描述", field: "productdescribe", align: "center"},
            {title: "单位", field: "amountunit", align: "center"},
            {title: "计划成本单价(元)", field: "realcostprice", align: "center"},
            {
                title: "操作", field: "productid", align: "center",
                formatter: function (vale, row, index) {
                    let ma = JSON.stringify(row);
                    let up_btn = "  <button class=\"btn btn-primary\" " +
                        " onclick='maAdd(" + ma + ")'>添加</button>";
                    return up_btn;
                }
            }
        ]
    })
}

// 刷新页面
function loadpage() {
    $("#tm01").bootstrapTable("refresh")
}

function maAdd(ma) {
    let lists = [];
    if (list.length < 1) {
        list.push(ma);
        load();
    } else {
        lists = list;
        for (let i = 0; i < lists.length; i++) {
            if (lists[i].productid == ma.productid) {
                return;
            }
        }
        list[list.length] = ma;
        load()
    }
}

function yl() {
    let shu = [];
    $(".amount").each(function () {
        shu.push($(this).val());
    })
    let dj = [];
    $(".costprice").each(function () {
        dj.push($(this).text());
    })
    let s = 0;
    $(".xiaoji").each(function () {
        $(this).text(parseInt(shu[s]) * parseInt(dj[s]))
        s = s + 1;
    })
}

function sut() {
    yl();
    let shu = [];
    $(".amount").each(function () {
        shu.push($(this).val());
    })
    for (let i = 0; i < shu.length; i++) {
        if (parseInt(shu[i]) <= 0) {
            alert("数量不能为小于等于0！")
            return;
        }
    }
    let xiaoji = [];
    $(".xiaoji").each(function () {
        xiaoji.push($(this).text());
    })
    let DfileOnt = JSON.parse(localStorage.getItem("DfileOnt"));

    $.post("/Dmodule.do/upDmgg", {id: DfileOnt.id}, function (sdata){
        let lss = [];
        for (let i = 0; i < list.length; i++) {
            let DModuleDetails = {
                "parentid": DfileOnt.id,
                "detailsnumber": list[i].detailsnumber,
                "productid": list[i].productid,
                "productname":list[i].productname,
                "type": list[i].type,
                "productdescribe": list[i].productdescribe,
                "amountunit": list[i].amountunit,
                "amount": shu[i],
                "costprice": list[i].costprice,
                "subtotal":xiaoji[i]
            };
            lss[i] = DModuleDetails;
        }
        console.log(lss)
        let str = JSON.stringify(lss);
        $.post("/Dmoduledetails.do/DelAdd", {lists: str}, function (sdate) {
            $("#staticBackdrop").modal("show");
            localStorage.removeItem("DfileOnt")
        })
    })
}

function hh() {
    location.href = "../../tan_kai/Dmgg.html";
}







